**********************************************************************
*Hospital year level regs (extensive)
**********************************************************************
	*File options 
	if $option ==1 {
	local is is
	local file cardio 
	}
	if $option ==2 {
	local is is
	local file preg
	}
	
*I. PREPARE REGRESSION FILE 
	*Data prep: This file assumes the construction of hospital-year level cardiac 
	*and delivery episode files from Elevance, which have been merged to 
	*American Hospital Association (AHA) IDs and mergers. These processing steps 
	*are described in Appendix A1-A4. 
	use "$reg\hospreg_`file'_inp_0_r0", clear
		
	*CONTROL VARS
	{
	local hc2 bdtot teach mcare mcaid type_gac 	
	local area rural white college unempl poverty elderly state_exp_status 	
	local hospcontrol `hc2' `area' hhi
	local control never 
	local treat (incl_1`is')
	local fe i.year
	local print keep(bought_1`is')
	}
	
	*DEFINE SAMPLE
	{
	*Merge to all AHA GAC hospitals 
	local get bought* incl* never absorb* ofint `hospcontrol' hrrcode type_gac ofint
	drop `get' pre* post*
	
	*aha_merge6_1.dta contains hospital-year level data from the AHA survey and 
	*identified mergers, described in Appendix A1, A3. 
	merge 1:1 num ye using "$aha\aha_merge6_1", keepusing(`get' hosp_cntrl)
	keep if type_gac==1
	drop if hosp_cntrl=="fed"
	drop if ye==2011
	drop _m	
	
	*Keep if is in an Elevance state of interest
	keep if ofint==1
	
	*Keep if has cardiac at some point during study period 
	replace vol = 0 if missing(vol)
	gen has_`file'2 	= (vol > 0)
	bysort num: gegen has_`file'     = max(has_`file'2)
	keep if has_`file'
	drop has_`file'*
	}
	
	*DEFINE TREATED, CONTROL GROUPS 
	{		
	*Event study vars  
	replace bought_1ss    = bought_2ss if bought_2ss==1
	replace incl_1ss      = incl_2ss if incl_2ss==1	
	foreach s in 1is 1ss {
	gen boughtyr1 = year if bought_`s'==1 & incl_`s'==1
	bysort num_prv: gegen boughtyr = min(boughtyr1)
	replace incl_`s'=0 if incl_`s'==1 & missing(boughtyr)

	gen pre3_`s' 	= (ye==boughtyr-3)
	gen pre2_`s' 	= (ye==boughtyr-2)
	gen pre1_`s' 	= (ye==boughtyr-1)
	gen pre0_`s' 	= (ye==boughtyr)
	gen post1_`s' 	= (ye==boughtyr+1)
	gen post2_`s' 	= (ye==boughtyr+2)
	gen post3_`s' 	= (ye>=boughtyr+3)
	gen post2_`s'2 = (ye>=boughtyr+2)
	gen post3_`s'2 = (ye==boughtyr+3)
	drop boughtyr*
	}
	
	*Drop hospitals that are absorbed/absorb another; with insufficient observations 
	drop if absorbing_flg==1 | absorbed_flg==1 
	
	gen t3samp`is' = (pre3_1`is'==1 | pre2_1`is'==1 | pre1_1`is'==1 | pre0_1`is'==1 | post1_1`is' ==1 | post2_1`is' ==1 | post3_1`is'2==1 | never==1)
	}
	
	*GENERATE VARS 
	{
	*Lagged HHI (HRR level) 
	*hhi_hrr_0.dta contains market concentration (HHI, calculated from bed shares
	*per Hospital Referral Region using AHA data) for each hospital-year.
	capture drop hhi 
	merge 1:1 num ye using "$aha\hhi_hrr_0", keepusing(hhi)
	xtset num ye	
	foreach var in hhi {
	ren `var' `var'2
	gen `var' = L1.`var'2
	}
	forval i = 1/4 {
	capture noisily replace hhi = L`i'.hhi if missing(hhi)
	capture noisily replace hhi = `var'2   if missing(hhi)
	}	
	drop *hhi*2
	drop if _m==2
	drop _m
	
	*Inflate 
	replace cost_tot_inp_n17  = cost_tot_inp_n17 / 0.9762
	}
	
	*LIMIT TO TREATED, CONTROL GROUPS WITH CARDIAC VOL 
	{
	keep if (incl_1`is' & t3samp`is') | never 
	
	*Keep if has 2+ years of data
	bysort num: gegen ct = count(ye)
	drop if ct==1
	drop ct
	bysort num: gegen sd = sd(bought_1`is')
	replace incl_1`is' = 0 if sd==0 
	drop sd 
	keep if (incl_1`is' & t3samp`is') | never 
	
	*Drop mergers that create new systems
	drop if flag_real_`is'c==1
	
	*Flag if added, dropped cardiac services
	gen has_`file'2 	= (vol > 0)
	gen has_`file'pre2  = (vol > 0 & bought_1`is'==0)
	gen has_`file'post2 = (vol > 0 & bought_1`is'==1)
	bysort num: gegen has_`file'     = max(has_`file'2)
	bysort num: gegen has_`file'pre  = max(has_`file'pre2)
	bysort num: gegen has_`file'post = max(has_`file'post2)
	
	*Keep if ever has cardiac volume 
	keep if has_`file'==1
	gen has_`file'prenew    = has_`file'2 & has_`file'pre
	gen has_`file'notprenew = has_`file'2 & !has_`file'pre
	drop has_`file'pre has_`file'post has_`file'
	ren has_`file'2 has_`file'
	}
	
*II. ANALYSIS
	capture rm "$op\extregs_`file'_inp`is'.xls"
	capture rm "$op\dvmean`file'_inp`is'.xls"
	xtset num_prv year
	
	*Table 5, A8, Figure A3(b)
	foreach var in has_`file' has_`file'prenew has_`file'notprenew {
	*DD
	qui xtreg `var' bought_1`is'    `hospcontrol' `fe' if (`treat' | `control'), fe vce(cluster num_prv)	
	outreg2 using "$op\extregs_`file'_inp`is'.xls", append `print' nocons ctitle("`is'_nowtcNDW_`var'") 

	*Event study
	qui xtreg `var' pre3_1`is' pre2_1`is' pre0_1`is' post1_1`is' post2_1`is' post3_1`is'2 `hospcontrol' `fe' if (`treat' | `control'), fe vce(cluster num_prv) 
	outreg2 using "$op\extregs_`file'_inp`is'.xls", append keep(pre* post*) nocons ctitle("`is'_nowtcNDW_otime3_`var'") 		
	test pre3_1`is' pre2_1`is' 
	gen pre = round(r(p),.01)
	
	*DV mean & pretrends
	qui tabout pre1_1`is' if (`treat' | `control') using "$op\dvmean`file'_inp`is'.xls", sum cells(mean `var' N `var' mean pre) f(3 0 2) total("`var'_ext") append
	drop pre 
	}